require('../common/style.css');
require('../common/header/index.css');
require('../common/nav-simple/index.css');
require('../common/footer/index.css');
require('./index.css')

require('./index.css');

//引入简单导航nav-simple
require('page/common/nav-simple/index.js');

var $ = require('jquery');
var _common_util = require('util/common_util.js');
var _user_service = require('service/user-service.js');

//错误信息的显示
var formError = {
    show: function (errMsg) {
        $('.error-item').show().find('.err-msg').text(errMsg);
    },
    hide: function () {
        $('.error-item').hide().find('.err-msg').text('');
    }
};


var page = {
    init: function () {
        this.bindEvent();
    },
    bindEvent: function () {
        var _this = this;
        $('#submit').click(function () {
            _this.submit();
            console.log("submit");
        })
    },
    submit: function () {
        var formData = {
            username: $.trim($('#username').val()),
            password: $.trim($('#password').val())
        };

        var validationResult = this.formValidation(formData);
        if (validationResult.status) {
            _user_service.login(
                formData,
                function (data, res) {
                    console.log(data)
                    window.location.href = _common_util.getURLParam('redirect') || './index.html';
                },
                function (errMsg) {
                    formError.show(errMsg);
                });
        } else {
            //提示数据校验错误
            formError.show(validationResult.msg);
        }
    },
    formValidation: function (formData) {
        var result = {
            status: false,
            msg: ''
        };

        if (!_common_util.validate(formData.username, 'require')) {
            result.msg = '用户名不能为空';
            return result;
        }
        if (!_common_util.validate(formData.password, 'require')) {
            result.msg = '密码不能为空';
            return result;
        }

        //验证通过
        result.status = true;
        result.msg = '验证通过';
        return result;
    }
};

$(function () {
    page.init();
});